package com.freemalll.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freemalll.common.core.utils.StringUtils;
import com.freemalll.common.core.utils.poi.ExcelUtil;
import com.freemalll.common.core.web.controller.BaseController;
import com.freemalll.common.core.web.domain.AjaxResult;
import com.freemalll.common.core.web.page.TableDataInfo;
import com.freemalll.common.log.annotation.Log;
import com.freemalll.common.log.enums.BusinessType;
import com.freemalll.common.security.annotation.RequiresPermissions;
import com.freemalll.common.security.utils.SecurityUtils;
import com.freemalll.system.domain.SysApplication;
import com.freemalll.system.service.ISysApplicationService;

/**
 * 系统应用 信息操作处理
 * 
 * @author freemalll
 */
@RestController
@RequestMapping("/application")
public class SysApplicationController extends BaseController
{
    @Autowired
    private ISysApplicationService applicationService;

    /**
     * 获取系统应用列表
     */
    @RequiresPermissions("system:application:list")
    @GetMapping("/list")
    public TableDataInfo list(SysApplication application)
    {
        startPage();
        List<SysApplication> list = applicationService.selectApplicationList(application);
        return getDataTable(list);
    }

    @Log(title = "系统应用", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:application:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysApplication application)
    {
        List<SysApplication> list = applicationService.selectApplicationList(application);
        ExcelUtil<SysApplication> util = new ExcelUtil<SysApplication>(SysApplication.class);
        util.exportExcel(response, list, "系统应用数据");
    }

    /**
     * 根据应用编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(applicationService.selectApplicationById(id));
    }

    /**
     * 根据应用编码查询应用信息
     */
    @GetMapping(value = "/appCode/{appCode}")
    public AjaxResult getInfoByAppCode(@PathVariable String appCode)
    {
        return success(applicationService.selectApplicationByAppCode(appCode));
    }

    /**
     * 新增系统应用
     */
    @RequiresPermissions("system:application:add")
    @Log(title = "系统应用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysApplication application)
    {
        if (!applicationService.checkAppCodeUnique(application))
        {
            return error("新增应用'" + application.getAppName() + "'失败，应用编码已存在");
        }
        if (StringUtils.isNotEmpty(application.getAppName()) && !applicationService.checkAppNameUnique(application))
        {
            return error("新增应用'" + application.getAppName() + "'失败，应用名称已存在");
        }
        application.setCreateName(SecurityUtils.getUsername());
        return toAjax(applicationService.insertApplication(application));
    }

    /**
     * 修改系统应用
     */
    @RequiresPermissions("system:application:edit")
    @Log(title = "系统应用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysApplication application)
    {
        if (!applicationService.checkAppCodeUnique(application))
        {
            return error("修改应用'" + application.getAppName() + "'失败，应用编码已存在");
        }
        if (StringUtils.isNotEmpty(application.getAppName()) && !applicationService.checkAppNameUnique(application))
        {
            return error("修改应用'" + application.getAppName() + "'失败，应用名称已存在");
        }
        application.setUpdateName(SecurityUtils.getUsername());
        return toAjax(applicationService.updateApplication(application));
    }

    /**
     * 删除系统应用
     */
    @RequiresPermissions("system:application:remove")
    @Log(title = "系统应用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        applicationService.deleteApplicationByIds(ids);
        return success();
    }
}